const path = require("path");
const HtmlWebpackPlugin=require("html-webpack-plugin")
const CopyWebpackPlugin=require("copy-webpack-plugin")
module.exports = {
    mode: "production",
    entry: path.resolve(__dirname, "../src/index.js"),
    output: {
        path: path.resolve(__dirname, "../build"),
        filename: "./js/build.js",
        //设置二级路由的路径为上级目录
        publicPath: "/"
    },
    module: {
        rules: [{
            test: /\.ejs$/i,
            loader: "ejs-loader",
            // ejs接收的数据
            options: {
                variable: "data",
            }
        }, {
            test: /\.css$/i,
            use: ["style-loader","css-loader"],
        }]
    },
    plugins: [new HtmlWebpackPlugin({
            //静态资源模板的路径
            template: path.resolve(__dirname, "../public/index.html"),
            //出口路径
            filename: "index.html",
            //压缩的配置
            minify: {
                //合并空白
                collapseWhitespace: true,
                //保持单标签的斜杠
                keepClosingSlash: true,
                //移除字符串中的注释
                removeComments: true,
                //删除多余的属性
                removeRedundantAttributes: true,
                //删除script标签的type属性
                removeScriptTypeAttributes: true,
                //删除link标签的type属性
                removeStyleLinkTypeAttributes: true,
                //使用简短的文档属性
                useShortDoctype: true
            },
        }), //静态公共资源拷贝
        new CopyWebpackPlugin({
            patterns: [{
                from: path.resolve(__dirname, "../public"),
                to: "public",
                globOptions: {
                    ignore: ["**/index.html"]
                }
            }],
        })
    ],
    resolve:{
        //配置别名
        alias:{
            $v:path.resolve(__dirname,"../src/views"),
            $r:path.resolve(__dirname,"../src/routers"),
            $a:path.resolve(__dirname,"../src/api")
        },
        // 配置后缀名，尝试按顺序解析这些后缀名。如果有多个文件有相同的名字，但后缀名不同，webpack 会解析列在数组首位的后缀的文件 并跳过其余的后缀。
        extensions: [".js", ".json", ".ejs"],
    }
}